// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: tianqiang

#pragma once

#include <grpcpp/grpcpp.h>

namespace coin2::exchange::base::api_base::grpc {

class XunkeMgmtAuth : public ::grpc::MetadataCredentialsPlugin {
 public:
  XunkeMgmtAuth(const ::grpc::string& xunkemgmt_api_key) : xunkemgmt_api_key_(xunkemgmt_api_key) {}

  ::grpc::Status GetMetadata(
      ::grpc::string_ref service_url,
      ::grpc::string_ref method_name,
      const ::grpc::AuthContext& channel_auth_context,
      std::multimap<::grpc::string, ::grpc::string>* metadata) override {
    metadata->emplace("xunkemgmt-api-key", xunkemgmt_api_key_);
    return ::grpc::Status::OK;
  }

  static void Composite(
      std::shared_ptr<::grpc::ChannelCredentials>* channel_creds,
      const std::string& token) {
    auto xunke_mgmt_auth = ::grpc::MetadataCredentialsFromPlugin(
        std::unique_ptr<::grpc::MetadataCredentialsPlugin>(new XunkeMgmtAuth(token)));
    *channel_creds = ::grpc::CompositeChannelCredentials(*channel_creds, xunke_mgmt_auth);
  }

 private:
  ::grpc::string xunkemgmt_api_key_;
};

class DefaultAuth {
 public:
  static void Composite(
      std::shared_ptr<::grpc::ChannelCredentials>* channel_creds,
      const std::string& token) {
    *channel_creds =
        ::grpc::CompositeChannelCredentials(*channel_creds, ::grpc::AccessTokenCredentials(token));
  }
};

}  // namespace coin2::exchange::base::api_base::grpc
